package org.chaiyi.template.core.common.exception;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum BaseErrorCode {
    CONTRACT_EXCEPTION("0001", "契约异常"),
    COLLECTION_METHOD_NOT_IMPLEMENT_EXCEPTION("0002", "collectionName()方法未实现"),
    SORT_ITEM_EXCEPTION("0003", "错误的排序条件"),
    KEY_NOT_FIND_EXCEPTION("0004", "未提供ID"),
    ENTITY_NOT_FIND_EXCEPTION("0005", "实体不存在"),
    HTTP_METHOD_INVALID("0006", "无效的http method"),

    ;
    private final String code;
    private final String msg;

    public BusinessError error() {
        return new BusinessError(code, msg).level(BusinessErrorType.NORMAL);
    }

    public BusinessError error(String level) {
        return new BusinessError(code, msg).level(level);
    }

    public void throwException() {
        throw error();
    }

    public void throwIf(boolean b) {
        if (b) throw error();
    }

    public void throwIf(boolean b, String level) {
        if (b) throw error(level);
    }
}
